package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.mapper.SetmealMapper;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.entity.SetmealDish;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import com.example.reggie.utils.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    //新增套餐，同时保存菜品与套餐关联关系
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(item->{
            item.setSetmealId(setmealDto.getId());
        });
        //保存套餐中的关联菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);

        //查询status是否为1(是否为起售套餐)
        lqw.eq(Setmeal::getStatus,1);
        //符合条件的个数
        int count = this.count(lqw);
        //如果个数大于0，则抛异常，不让删除
        if (count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果不是，先删除setmeal表中数据
        this.removeByIds(ids);
        //再删除setmeal_dish表中的关联数据
        LambdaQueryWrapper<SetmealDish> dishLqw = new LambdaQueryWrapper<>();
        dishLqw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(dishLqw);
    }

    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateStatus(List<Long> ids,int code) {
        List<Setmeal> list = new ArrayList<>();
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(code);
            list.add(setmeal);
        }
        this.updateBatchById(list);
    }

    @Override
    public IPage<SetmealDto> toDtoPage(int page, int pageSize, String name) {
        IPage<Setmeal> dishpage = new Page(page,pageSize);
        IPage<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null,Setmeal::getName,name);
        page(dishpage,lqw);
        BeanUtils.copyProperties(dishpage,dtoPage);
        List<Setmeal> records = dishpage.getRecords();
        List<SetmealDto> dtoRecords = records.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());

            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(dtoRecords);
        return dtoPage;
    }

    @Override
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public List<Setmeal> toList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = list(lqw);
        return list;
    }


}
